package com.a.learning_keep.activity.time

import android.animation.ValueAnimator
import android.app.TimePickerDialog
import android.content.Context
import android.os.CountDownTimer
import android.view.View
import android.widget.Button
import com.a.learning_keep.R
import com.a.learning_keep.base.BaseActivity
import com.a.learning_keep.databinding.ActivityTimerControlInterfaceBinding


class TimerControlActivity : BaseActivity<ActivityTimerControlInterfaceBinding>() {

    override fun getBind(): ActivityTimerControlInterfaceBinding {
        return ActivityTimerControlInterfaceBinding.inflate(layoutInflater)
    }

    override fun initView() {
        setTit("计时器调节")
        //选择时间
        binding.mianTime.setOnClickListener {
            timeSelect(this, binding)
        }
        binding.nerCircleProView.setOnClickListener {
            timeSelect(this, binding)
        }

        //开始计时
        binding.startButton.setOnClickListener {
            startTimer()
        }
        //暂停计时
        binding.stopButton.setOnClickListener {
           pauseTimer()
        }

        //取消计时
        binding.pauseButton.setOnClickListener {
            this.binding.mianTime.text = "00:00"
            this.binding.nerCircleProView.setValueAnimator(0)

            this.binding.startButton.visibility = View.GONE
            this.binding.but2.visibility = View.GONE

        }
    }

    // 开始计时
    private fun startTimer() {

    }

    //暂停计时
    private fun pauseTimer() {

    }


    /**
     * 设置点击动画化
     * */
    private fun startAnimation(button: Button) {
        val initialWidth = button.width
        val targetWidth = 100 // 目标宽度为0，即按钮最终消失

        val animator = ValueAnimator.ofInt(initialWidth, targetWidth)
        animator.addUpdateListener { animation ->
            val animatedWidth = animation.animatedValue as Int
            val layoutParams = button.layoutParams
            layoutParams.width = animatedWidth
            button.layoutParams = layoutParams
        }

        animator.duration = 1000 // 动画持续时间为1秒
        animator.start()
    }


    /**
     * 创建一个时间选择窗口
     * */
    private fun timeSelect(context: Context, binding: ActivityTimerControlInterfaceBinding) {
        val timePickerDialog = TimePickerDialog(
            context,
            R.style.CustomTimePickerDialog,
            { _, hourOfDay, minute ->
                val selectedTime = String.format("%02d:%02d", hourOfDay, minute)
                this.binding.mianTime.text = selectedTime
                this.binding.nerCircleProView.setValueAnimator(100)
                this.binding.startButton.visibility = View.VISIBLE
            },
            12,
            0,
            true
        )
        timePickerDialog.show()

    }
}